/**
 * 广告支出-单日展示
 */   
$(function(){
	$("#searchBtn").on("click",function(){
		byadTotalPays(startTime.value,endTime.value);
	})
	
	var endTime = getQueryString("endTime");
	var startTime=getQueryString("startTime");
	var page;
	if(page==null || page.length<=0){
		page=1;
	};	
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		startTime=$("#startTime").val();
		endTime=$("#endTime").val();
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					byadTotalPays(startTime,endTime);
				}else if(page>showPageN-4){
					
				}else{
					total(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
	
	if(endTime&&startTime){
		$('#adTotal').find("tr").eq(0).siblings().remove();
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/byDays',
	        data:{"endTime":endTime,"startTime":startTime,"page":page},
			type : 'post',
			success : function(data) {
				
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				var beans = data.beans;
				var avgs=0;
				var clickNums=0;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var MyPays=0;


				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byadTotalPays2(page);	
		            }
				});
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					if(beans[i].accountStatus==1){
						beans[i].accountStatus="冻结";
					}
					if(beans[i].accountStatus==0){
						beans[i].accountStatus="正常";
					}
					if(beans[i].accountStatus==null){
						beans[i].accountStatus="正常";
					}
					var tbody = "<tr><td>"+(i+1)+"</td>" +
									"<td>"+beans[i].time +"</td>" +
									"<td>"+beans[i].accountStatus+"</td>" +
									"<td>"+(beans[i].averagePirce*1).toFixed(2)+"</td>" +
									"<td>"+beans[i].clickNum+"</td>" +
									"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
									"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
									"<td>"+beans[i].adBudget+"</td>" +
									"<td>"+((page-1)*5+(i+1))+"</td></tr>";
									
					$('#adTotal').append(tbody);
				}
				var allBean=data.allBean;
				for (var i = 0; i < allBean.length;i++) {
					MyPays+=allBean[i].adPay*1;
					var proportion=(allBean[i].proportion*100).toFixed(2);
					proportions+=proportion*1;
					adBudgets+=allBean[i].adBudget*1;
					clickNums+=allBean[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" + 
			 "</tr>";
				if(data.pages==data.pageNum){
					$('#adTotal').append(tbodyTwo);
				}
				
			},
	        error: function(){}        
		});
	}
});

function total(page){
	startTime=$("#startTime").val();
	endTime=$("#endTime").val();
	if(endTime&&startTime){
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/byDays',
	        data:{"endTime":endTime,"startTime":startTime,"page":page},
			type : 'post',
			success : function(data) {
				
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				var beans = data.beans;
				var MyPays=0;
				var avgs=0;
				var clickNums=0;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				//总支出
				var sumPay = data.sumPay;
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byadTotalPays2(page);	
		            }
				});
			},
	        error: function(){}        
		});
	}
}

//查詢按鈕所查詢的數據
function byadTotalPays(startTime,endTime){
	$('#adTotal').find("tr").eq(0).siblings().remove();
	var page;
	if(page==null || page.length<=0){
		var page=1;
	}
	$.ajax({
		url : '/Lvscore_Advertiser_Maven/byDays',
        data:{"endTime":endTime,"startTime":startTime,"page":page},
		type : 'post',
		success : function(data) {
			var beans = data.beans;
			var MyPays=0;
			var avgs=0;
			var clickNums=0;
			var proportions=0;
			var recharges=0;
			var adBudgets=0;
			//总支出
			var sumPay = data.sumPay;
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	byadTotalPays2(page);	
	            }
			});
			
			//循环输出表格
			for(var i = 0; i < beans.length;i++){
				if(beans[i].accountStatus==1){
					beans[i].accountStatus="冻结";
				}
				if(beans[i].accountStatus==0){
					beans[i].accountStatus="正常";
				}
				if(beans[i].accountStatus==null){
					beans[i].accountStatus="正常";
				}
				var tbody = "<tr><td>"+(i+1)+"</td>" +
				"<td>"+beans[i].time +"</td>" +
				"<td>"+beans[i].accountStatus+"</td>" +
				"<td>"+(beans[i].averagePirce*1).toFixed(2)+"</td>" +
				"<td>"+beans[i].clickNum+"</td>" +
				"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
				"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
				"<td>"+beans[i].adBudget+"</td>" +
				"<td>"+((page-1)*5+(i+1))+"</td></tr>";
				
				$('#adTotal').append(tbody);
			}
			var allBean=data.allBean;
			for (var i = 0; i < allBean.length;i++) {
				MyPays+=allBean[i].adPay*1;
				var proportion=(allBean[i].proportion*100).toFixed(2);
				proportions+=proportion*1;
				adBudgets+=allBean[i].adBudget*1;
				clickNums+=allBean[i].clickNum*1;
			}
			var tbodyTwo="<tr>" +
			"<td>总计</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>---</td>" +
			"<td>"+clickNums+"</td>" +
			"<td>"+MyPays.toFixed(2)+"</td>" +
		    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
		    "<td>"+adBudgets.toFixed(2)+"</td>" +
		    "<td>---</td>" + 
		 "</tr>";
			if(data.pages==data.pageNum){
				$('#adTotal').append(tbodyTwo);
			}
		},
        error: function(){}        
	});
}

function byadTotalPays2(page){
	var endTime = $("#endTime").val();
	var startTime=$("#startTime").val();
	if(endTime&&startTime){
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/byDays',
	        data:{"endTime":endTime,"startTime":startTime,"page":page},
			type : 'post',
			success : function(data) {
				
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				var beans = data.beans;
				var MyPays=0;
				var avgs=0;
				var clickNums=0;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					if(beans[i].accountStatus==1){
						beans[i].accountStatus="冻结";
					}
					if(beans[i].accountStatus==0){
						beans[i].accountStatus="正常";
					}
					if(beans[i].accountStatus==null){
						beans[i].accountStatus="正常";
					}
					var tbody = "<tr><td>"+(i+1)+"</td>" +
									"<td>"+beans[i].time +"</td>" +
									"<td>"+beans[i].accountStatus+"</td>" +
									"<td>"+(beans[i].averagePirce*1).toFixed(2)+"</td>" +
									"<td>"+beans[i].clickNum+"</td>" +
									"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
									"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
									"<td>"+beans[i].adBudget+"</td>" +
									"<td>"+((page-1)*5+(i+1))+"</td></tr>";
								
				}
				var allBean=data.allBean;
				for (var i = 0; i < allBean.length;i++) {
					MyPays+=allBean[i].adPay*1;
					var proportion=(allBean[i].proportion*100).toFixed(2);
					proportions+=proportion*1;
					adBudgets+=allBean[i].adBudget*1;
					clickNums+=allBean[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" + 
			 "</tr>";
				if(data.pages==data.pageNum){
					$('#adTotal').append(tbodyTwo);
				}
			},
	        error: function(){}        
		});
	}
}
